package com.estronger.shopmanage.adapter.tasking;

import android.Manifest;
import android.app.Dialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.estronger.shopmanage.R;
import com.estronger.shopmanage.bean.DeliverDPG;
import com.estronger.shopmanage.ui.send.pg.DeliverAppointmentActivity;
import com.estronger.shopmanage.ui.send.pg.DeliverDetailActivity;
import com.estronger.shopmanage.ui.send.pg.DeliverPGActivity;
import com.estronger.shopmanage.utils.ConvertUtils;
import com.estronger.shopmanage.utils.PermissionUtils;
import com.estronger.shopmanage.utils.TimeUtils;
import com.estronger.shopmanage.view.AppDialog;

import java.util.ArrayList;

import cn.jesse.nativelogger.NLogger;

import static com.estronger.shopmanage.utils.UiUtils.startActivity;


public class DeliverDPGListAdapter extends BaseAdapter implements OnClickListener {

    private DeliverPGActivity mContext;
    private ArrayList<DeliverDPG.Datas.DeliverDPGListData> mList;
    public Dialog dialog;
    private int whitchToCall = 0;//是点击了那个按钮后跳出打电话对话框的

    public DeliverDPGListAdapter(DeliverPGActivity mContext, ArrayList<DeliverDPG.Datas.DeliverDPGListData> lt) {
        this.mContext = mContext;
        this.mList = lt;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        if (mList != null)
            return mList.size();
        else
            return 0;
    }

    @Override
    public DeliverDPG.Datas.DeliverDPGListData getItem(int position) {
        // TODO Auto-generated method stub
        if (mList == null || mList.size() <= 0)
            return null;
        else
            return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        // TODO Auto-generated method stub
        final ListItemView ltv;

        if (view == null) {
            view = LayoutInflater.from(mContext).inflate(
                    R.layout.adpter_deliver_daipai, null);
            ltv = new ListItemView();
            ltv.deliversname = (TextView) view.findViewById(R.id.deliversname);
            ltv.delive_itemcount = (TextView) view.findViewById(R.id.delive_itemcount);
            ltv.deliver_item_location = (TextView) view.findViewById(R.id.deliver_item_location);
            ltv.deliver_item_time = (TextView) view.findViewById(R.id.deliver_item_time);
            ltv.deliver_phone_call = (ImageView) view.findViewById(R.id.deliver_phone_call);
            ltv.deliver_send_sms = (ImageView) view.findViewById(R.id.deliver_send_sms);
            ltv.ll_detail = (LinearLayout) view.findViewById(R.id.ll_detail);
            ltv.deliver_item_receive = (LinearLayout) view.findViewById(R.id.deliver_item_receive);
            ltv.deliver_item_phoneno = (TextView)view.findViewById(R.id.deliver_item_phoneno);

            //去详情
            ltv.ll_detail.setOnClickListener(this);
            ltv.ll_detail.setTag(ltv);
            ltv.deliver_phone_call.setOnClickListener(this);
            ltv.deliver_phone_call.setTag(ltv);
            ltv.deliver_send_sms.setOnClickListener(this);
            ltv.deliver_send_sms.setTag(ltv);
            ltv.deliver_item_receive.setOnClickListener(this);
            ltv.deliver_item_receive.setTag(ltv);

            view.setTag(ltv);
        } else {
            ltv = (ListItemView) view.getTag();
        }

        try {
            ltv.deliversname.setText(ConvertUtils.formatNullF(mList.get(position).buildname) + ConvertUtils.formatNull(mList.get(position).cstname));
            ltv.delive_itemcount.setText(ConvertUtils.formatNull(mList.get(position).dlvqty));
            ltv.deliver_item_location.setText(ConvertUtils.formatNull(mList.get(position).address));
            ltv.deliver_item_time.setText(TimeUtils.cutsecond(mList.get(position).cdate));
            ltv._phone = mList.get(position).cstmobile;
            ltv.TitelName = ltv.deliversname.getText().toString();
            ltv._dlvid = mList.get(position).dlvid;
            ltv._singleData = mList.get(position);
            ltv.deliver_item_phoneno.setText(ConvertUtils.formatNull(mList.get(position).cstmobile));

        } catch (Exception e) {
            e.printStackTrace();
        }

        return view;
    }

    class ListItemView {
        public String TitelName = "";
        public TextView deliversname, delive_itemcount, deliver_item_location, deliver_item_time,deliver_item_phoneno;
        public ImageView deliver_phone_call, deliver_send_sms;
        public LinearLayout ll_detail, deliver_item_receive;
        public String _phone = "";
        public String _dlvid = "";
        public DeliverDPG.Datas.DeliverDPGListData _singleData;
    }

    public String phone;
    public String dlvid;
    public DeliverDPG.Datas.DeliverDPGListData singleData;

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.ll_detail:
                ListItemView lv = (ListItemView) v.getTag();
                intent = new Intent(mContext, DeliverDetailActivity.class);
                intent.putExtra("dlvid", lv._dlvid);
                intent.putExtra("singleData", lv._singleData);
                mContext.startActivity(intent);
                break;
            case R.id.deliver_phone_call:// 点击了测量条目上的电话小图标
                lv = (ListItemView) v.getTag();
                phone = lv._phone;
                dlvid = lv._dlvid;
                if (mContext.isRightNumber(phone)) {
                    dialog = AppDialog.createPgDialog(mContext,
                            "[" + lv.TitelName + "]", phone, this, "");
                    dialog.show();
                }
                whitchToCall = 0;
                break;
            case R.id.deliver_send_sms:// 点击了测量条目上的发短信小图标
                lv = (ListItemView) v.getTag();
                phone = lv._phone;
                dlvid = lv._dlvid;
                if (mContext.isRightNumber(phone)) {
                    Uri uri = Uri.parse("smsto:" + phone);
                    intent = new Intent(Intent.ACTION_SENDTO, uri);
                    mContext.startActivity(intent);
                }
                break;
            case R.id.deliver_item_receive:
                lv = (ListItemView) v.getTag();
                phone = lv._phone;
                dlvid = lv._dlvid;
                singleData = lv._singleData;
                if (mContext.isRightNumber(phone)) {
                    dialog = AppDialog.createPgDialog(mContext,
                            "[" + lv.TitelName + "]", phone, this, "");
                    dialog.show();
                }
                whitchToCall = 1;
                break;
            case R.id.btnfSure:
                try {
                    if (PermissionUtils.isCallPhoneGranted(mContext)) {
                        intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"
                                + phone));
                        if (whitchToCall == 1) {// 这里是判断拨打电话后是否需要跳转其它界面
                            DeliverPGActivity.CALL_RESULT = 1313;
                            DeliverPGActivity.dlvid = dlvid;
                            DeliverPGActivity.singleData = singleData;
                            if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                                // TODO: Consider calling
                                //    ActivityCompat#requestPermissions
                                // here to request the missing permissions, and then overriding
                                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                                //                                          int[] grantResults)
                                // to handle the case where the user grants the permission. See the documentation
                                // for ActivityCompat#requestPermissions for more details.
                                return;
                            }
                            mContext.startActivityForResult(intent,
                                    DeliverPGActivity.CALL_RESULT);
                        } else {
                            mContext.startActivity(intent);
                        }
                        dialog.dismiss();
                    } else {
                        PermissionUtils.ToSetPermissions(mContext, R.string.PermissionsCallPhone);
                        dialog.dismiss();
                    }
                } catch (Exception e) {
                    NLogger.e("" + e.getMessage());
                }
                break;
            case R.id.btnfCancel:
                if (whitchToCall == 1) {
                    DeliverPGActivity.CALL_RESULT = 1313;
                    DeliverPGActivity.dlvid = "";
                    DeliverPGActivity.singleData = singleData;
                    Intent intent1 = new Intent(mContext, DeliverAppointmentActivity.class);
                    intent1.putExtra("singleData",singleData);
                    startActivity(intent1);
                }
                dialog.dismiss();
        }
    }
}
